import {
	setUserInfo,
	getUserInfo,
	// setJsCode,
	// getJsCode
} from "@/utils/storageSync"
import {base_url} from "../services/api"
function imgUrl(imageUrl) {
	console.log(imageUrl)
	console.log(base_url + imageUrl)
	if(imageUrl ){
		return base_url + imageUrl
	}else{
		return base_url +  '/images/company/imgError.png'
	}
}



function imagesBig(tempFilePaths, current = 0) {
	let imgsArray = [];
	if (typeof tempFilePaths == "object") {
		console.log(tempFilePaths)
		imgsArray = tempFilePaths
	} else {
		imgsArray[0] = tempFilePaths;
	}
	// console.log(current)
	uni.previewImage({
		current: current,
		urls: imgsArray
	});
}

//登录获取jscode
 function loginApplet() {


    // let check =  await checkSession()
    // if(!check || !getJsCode()){
		return  new Promise(
			function(resolve, reject) {
				uni.login({
					provider:
					// #ifdef MP-WEIXIN 
						"weixin"
					// #endif
					// #ifdef MP-TOUTIAO
					"toutiao"
					// #endif
					,
					complete: function(e) {
						if (e.errMsg == "login:ok") {
							// setJsCode(e.code)
							// console.log(e)
							resolve(e.code)
						} else {
							resolve(false)
						}
					}
				});
			})
}

function checkSession(){
  return  new Promise((resolve, reject)=> {
	    uni.checkSession({
			complete: (res) => {
				if(res.errMsg ==  "checkSession:ok"){
					resolve(true)
				}else{
					resolve(false)
				}
			}
		})
		})	
	
}
function randomEnd(start,end){
return Math.floor(Math.random() * (end - start) + start)
}
function checkField(field){
	
	if(field == null || field == '' || typeof field == 'undefined' ){
		return false
	}else{
		return true
	}
}



//秒数转换成 多久前
function secondsChange(sec){
	let secFormatting = Number(sec).toFixed(0)
	if(secFormatting < 60){
		return `${sec}秒前` 
	}else if(secFormatting < 3600){
		return `${Math.floor(secFormatting/60)}分钟前`
	}else if(secFormatting >= 3600){
		return `${Math.floor(secFormatting/3600)}小时前`
	}
}


//当前时间减去 现在时间相差的秒数
function getSecondByDateSub(begin,end = "") {
    var beginDate  = new Date(begin);
    var endDate = end?new Date(end):new Date();
    var diff = endDate.getTime() - beginDate.getTime();
    var sec = diff /1000;
    return  sec ;
}




export default {


	install: function(Vue) {
		Vue.prototype.imgUrl = (imageUrl) => imgUrl(imageUrl);
		Vue.prototype.imagesBig = (tempFilePaths, current) => imagesBig(tempFilePaths, current);
		Vue.prototype.loginApplet = () => loginApplet();
		Vue.prototype.getUserInfo = () => getUserInfo();
		Vue.prototype.setUserInfo = (userInfo) => setUserInfo(userInfo);
		Vue.prototype.checkField = (field) => checkField(field);
		Vue.prototype.getSecondByDateSub = (begin,end) => getSecondByDateSub(begin,end);
		Vue.prototype.secondsChange = (sec) => secondsChange(sec);
		Vue.prototype.randomEnd = (start,end) => randomEnd(start,end);
		
		
		// Vue.prototype.all_currency = () => all_currency();
		// Vue.prototype.islogin = () => islogin();
		// Vue.prototype.loading = () => loading();
		// Vue.prototype.message = (str) => message(str);
		// Vue.prototype.payment = (str, Serial) => payment(str, Serial);
		// Vue.prototype.formatSeconds = (value) => formatSeconds(value);
		// Vue.prototype.deepClone = (obj) => deepClone(obj);
		// Vue.prototype.formateData = (data,name) => formateData(data,name);
		// Vue.prototype.traditionalChinese = (str) => traditionalChinese(str);
	},
}
